﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;

namespace cadbom
{
    public partial class SetProcessName : Form
    {
        Action<String> callback;
        List<String> itemList;
        int selectedIndex;
        public SetProcessName()
        {
            InitializeComponent();
            InitializeCheckedListBox();
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void InitializeCheckedListBox()
        {
            // 假设有一些选项
            //checkedListBox1.Items.Add("选项1");
            //checkedListBox1.Items.Add("选项2");
            //checkedListBox1.Items.Add("选项3");
            checkedListBox1.DataSource = itemList;
            // 设置CheckOnClick为true
            checkedListBox1.CheckOnClick = true;

            // 添加ItemCheck事件处理程序
            checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
        }
        public void initData(Action<String> callback,List<String> itemList) {
            this.callback = callback;
            this.itemList = itemList;
            checkedListBox1.DataSource = itemList;
        }
        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            // 如果当前项被选中，则取消其他项的选中状态
            if (e.NewValue == CheckState.Checked)
            {
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    if (i != e.Index)
                    {
                        checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
                    }
                }
                selectedIndex = e.Index;                            
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            callback.Invoke(checkedListBox1.Items[selectedIndex].ToString());
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
